package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper,Student> implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Lazy
    @Autowired
    private ClazzService clazzService;

    //4.1 学员列表查询
    @Override
    public StudentResult pageStu(StudentQueryParam queryParam) {
        //添加条件
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(queryParam.getName()!=null && !queryParam.getName().isEmpty(),Student::getName,queryParam.getName())
                .eq(queryParam.getDegree()!=null,Student::getDegree,queryParam.getDegree())
                .eq(queryParam.getClazzId()!=null,Student::getClazzId,queryParam.getClazzId());
        //获取分页参数
        Page<Student> page = Page.of(queryParam.getPage(),queryParam.getPageSize());
        page = this.page(page, queryWrapper);

        List<Student> stus = page.getRecords();
        List<Integer> clazzIds = stus.stream().map(Student::getClazzId)
                .toList();
        if(!clazzIds.isEmpty()){
            //查询班级名称
            List<Clazz> clazzes = clazzService.listByIds(clazzIds);
            stus.forEach(stu->{
                clazzes.forEach(clazz->{
                    if(stu.getClazzId()!=null && stu.getClazzId().equals(clazz.getId()))
                        stu.setClazzName(clazz.getName());
                });
            });
        }
        return new StudentResult(page.getTotal(), stus);
    }




/*    //4.1 学员列表查询
    @Override
    public StudentResult getPage(StudentQueryParam queryParam) {
        return null;
    }

    //4.2 删除学员
    @Override
    public void deleteByIds(List<Integer> ids) {
        //删除学员的信息
        studentMapper.deleteByIds(ids);
    }

    //4.3 添加学员
    @Override
    public void insertStudent(Student student) {
        //补全基础信息
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        //调用持久层，传入参数
        studentMapper.insertStudent(student);

    }

    //4.4 根据id查询
    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    //4.5 修改学员
    @Override
    public void update(Student student) {
        //填补更新时间
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);

    }

    //4.6 违纪处理
    @Override
    public void updateViolation(Integer id, Integer score) {
        short count =studentMapper.getById(id).getViolationCount();
        Integer violationCount = (int)count+1;
        short score1= studentMapper.getById(id).getViolationScore();
        Integer violationScore = (int)score1+score;
        studentMapper.updateViolation(id,violationCount,violationScore);

    }*/
}
